/**
 * Created by tsy on 2016/7/13.
 */

$(function(){
    window.onload = function(){
        menuFixed('form-link');
    }
    $('#defaultForm').validationEngine();
});


function menuFixed(id){
    var obj = document.getElementById(id);
    var _getHeight = obj.offsetTop;

    window.onscroll = function(){
        changePos(id,_getHeight);
    }
}
function changePos(id,height){
    var obj = document.getElementById(id);
    var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
    if(scrollTop < height){
        obj.style.top='0';
        obj.style.left='0';
        obj.style.position = 'relative';
    }else{
        obj.style.position = 'fixed';
    }
}


function show_fac(value){
    if(value==1){
        $("#fac_body").show();
    }else{
        $("#fac_body").hide();
    }
}
function pagerFilter(data){
    if (typeof data.length == 'number' && typeof data.splice == 'function'){	// is array
        data = {
            total: data.length,
            rows: data
        }
    }
    var dg = $(this);
    var opts = dg.datagrid('options');
    var pager = dg.datagrid('getPager');
    pager.pagination({
        onSelectPage:function(pageNum, pageSize){
            opts.pageNumber = pageNum;
            opts.pageSize = pageSize;
            pager.pagination('refresh',{
                pageNumber:pageNum,
                pageSize:pageSize
            });
            dg.datagrid('loadData',data);
        }
    });
    if (!data.originalRows){
        data.originalRows = (data.rows);
    }
    var start = (opts.pageNumber-1)*parseInt(opts.pageSize);
    var end = start + parseInt(opts.pageSize);
    data.rows = (data.originalRows.slice(start, end));
    return data;
}
//function onClickCell(index, field){
//    var row = $('#fac_table').datagrid('getSelected');
//    console.log(row);
//    //if(row){
//    //    $("#fac_simple_name").textbox('setValue',row.fac_simple_name);
//    //    $("#fac_id").val(row.fac_account);
//    //}
//}
function fac_check_ok(){
    var checkedItems = $('#fac_table').datagrid('getChecked');
    $("#load_name").val(checkedItems[0].fac_corporation_email);
    $("#user_phone").val(checkedItems[0].fac_corporation_phone);
    $("#user_name").val(checkedItems[0].fac_simple_name);
    $("#fac_id").val(checkedItems[0].id);
}

function show_fac(obj){
    var value=obj.checked;
    if(value){
        $("#fac_div").css("display","block");
        $("#sms_tr").show();
        show_fac_list()
    }else{
        $("#fac_div").css("display","none");
        $("#sms_tr").hide();
    }
}

function show_fac_list(){
    var toolbar = [{
        text:'确定',
        iconCls:'icon-save',
        handler:fac_check_ok
    }];
    $('#fac_table').datagrid({
        url: '../../facilitator/facilitator/select_facilitator_info',
        method: 'get',
        iconCls: 'icon-save',
        width: "100%",
        height: 360,
        fitColumns: true,
        title:"商户列表",
        columns:[[
            {field:'id',checkbox:true,width:20},
            {field:'fac_account',title:'商户编码',width:80},
            {field:'name',title:'商户类型',width:50},
            {field:'fac_simple_name',title:'商户简称',width:120},
            {field:'fac_corporation_phone',title:'法人电话',width:100},
            {field:'fac_corporation_email',title:'法人邮箱',width:120}
        ]],
        loadFilter:pagerFilter,
        rownumbers:true,
        singleSelect:true,
        autoRowHeight:false,
        pagination:true,
        pageSize:10,
        toolbar:toolbar
    });
}

function reset(){
    $("#defaultForm")[0].reset();
}

function close_tab(){
    var title="用户管理";
    var url="../../Admin/user/user_main";
    window.parent.removeTab(title,url);
}

function add_ajax_submit(){
    if(!$("form#defaultForm").validationEngine("validate")){
        return false;
    };
    var url="../../Admin/User/user_add_ajax";
    $.ajax({
        type: "post",
        async: true,
        data: $("#defaultForm").serialize(),
        url: url,
        success: function(d){
            console.log(d);
            var data=$.parseJSON(d);
            if(data.state==1){
                alert("新增用户成功！");
                window.parent.removeTab("用户管理","../../Admin/user/user_main");
            }else{
                alert("新增用户失败！");
                window.location.reload();
            }
        },
        error: function(e){
            alert("网络错误，请联系管理员");
        }
    });
}

function doSearch(){
    var queryParams = $('#fac_table').datagrid('options').queryParams;
    var fac_name=$("#fac_name").val();
    queryParams.fac_name = fac_name;
    $('#fac_table').datagrid('options').queryParams=queryParams;
    $("#fac_table").datagrid('reload');
}